#!/usr/bin/perl -w
use strict;

use Bio::SeqIO;

my $in = Bio::SeqIO->new(-format=>'fasta',
						 -file  => shift @ARGV);
my $out = Bio::SeqIO->new(-format => 'fasta');
my %seqs;
while(my $seq = $in->next_seq ) {
 my $id = $seq->id;
 my $desc = $seq->description;
 my $gene_name;
 if( $desc =~ s/\|\s+(\S+)\s+// ) {
   $gene_name = $1;
 } else {
   warn ("cannot parse the gene name from $desc\n");
 }
 $seq->id($gene_name);
 $seq->description("| $id $desc");
 my $seqstr = $seq->seq;
 if( exists $seqs{$gene_name} ) {
    if( $seqs{$gene_name}->length < $seq->length) {
      $seqs{$gene_name} = $seq;
    }
 } else {
   $seqs{$gene_name} = $seq;
 }
}
# could do this but it will be unordered
#while( my ($seqname,$seq) = each %seqs ) {
#}

for my $seqname ( sort keys %seqs ) {
	my $seq = $seqs{$seqname};
	$out->write_seq($seq);
}